package com.ruoyi.docdraw.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.docdraw.domain.SysSubProject;
import com.ruoyi.docdraw.domain.subProject.SysSubProjectVo;
import com.ruoyi.docdraw.domain.subProjectFolder.SysSubProjectFolderVo;
import com.ruoyi.docdraw.service.ISysGDPService;
import com.ruoyi.docdraw.service.ISysSubProjectService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.docdraw.domain.SysSubProjectFolder;
import com.ruoyi.docdraw.service.ISysSubProjectFolderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 子项目文件夹Controller
 * 
 * @author lvtian
 * @date 2025-04-14
 */
@RestController
@RequestMapping("/docdraw/subProjectFolder")
public class SysSubProjectFolderController extends BaseController
{
    @Autowired
    private ISysSubProjectFolderService sysSubProjectFolderService;

    @Autowired
    private ISysSubProjectService sysSubProjectService;

    @Autowired
    private ISysGDPService sysGDPService;

    /**
     * 查询子项目文件夹列表
     */
    @PreAuthorize("@ss.hasPermi('docdraw:subProjectFolder:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysSubProjectFolder sysSubProjectFolder)
    {
        startPage();
        List<SysSubProjectFolder> list = sysSubProjectFolderService.selectSysSubProjectFolderList(sysSubProjectFolder);
        List<SysSubProjectFolderVo> vos = new ArrayList<>();
        for (SysSubProjectFolder subProjectFolder : list) {
            Long subProjectId = subProjectFolder.getSubProjectId();
            SysSubProjectFolderVo sysSubProjectFolderVo = new SysSubProjectFolderVo();
            BeanUtils.copyBeanProp(sysSubProjectFolderVo, subProjectFolder);

            SysSubProject subProject = sysSubProjectService.selectSysSubProjectBySubProjectId(subProjectId);
            SysSubProjectVo sysSubProjectVo = sysGDPService.getSysSubProjectVo(subProject);
            sysSubProjectFolderVo.setSysSubProjectVo(sysSubProjectVo);
            vos.add(sysSubProjectFolderVo);
        }
        return getDataTable(vos);
    }

    /**
     * 导出子项目文件夹列表
     */
    @PreAuthorize("@ss.hasPermi('docdraw:subProjectFolder:export')")
    @Log(title = "子项目文件夹", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysSubProjectFolder sysSubProjectFolder)
    {
        List<SysSubProjectFolder> list = sysSubProjectFolderService.selectSysSubProjectFolderList(sysSubProjectFolder);
        ExcelUtil<SysSubProjectFolder> util = new ExcelUtil<SysSubProjectFolder>(SysSubProjectFolder.class);
        util.exportExcel(response, list, "子项目文件夹数据");
    }

    /**
     * 获取子项目文件夹详细信息
     */
    @PreAuthorize("@ss.hasPermi('docdraw:subProjectFolder:query')")
    @GetMapping(value = "/{subFolderId}")
    public AjaxResult getInfo(@PathVariable("subFolderId") Long subFolderId)
    {
        return success(sysSubProjectFolderService.selectSysSubProjectFolderBySubFolderId(subFolderId));
    }

    /**
     * 新增子项目文件夹
     */
    @PreAuthorize("@ss.hasPermi('docdraw:subProjectFolder:add')")
    @Log(title = "子项目文件夹", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysSubProjectFolder sysSubProjectFolder)
    {

        // 检查子项目是否停用
        Long subProjectId = sysSubProjectFolder.getSubProjectId();
        SysSubProject sysSubProject = sysSubProjectService.selectSysSubProjectBySubProjectId(subProjectId);
        String status = sysSubProject.getStatus();
        if(UserConstants.USER_DISABLE.equals(status)){
            return error("项目 ["+sysSubProject.getSubProjectName()+"] 已被暂停 请联系管理员");
        }

        // 检查在同一子项目下是否存在重名
        if (!sysSubProjectFolderService.checkSubProjectFolderNameUnique(sysSubProjectFolder)) {
            return error("项目 ["+sysSubProject.getSubProjectName()+"] 已存在同名文件夹 请见检查");
        }


        sysSubProjectFolder.setCreateBy(getUsername());
        return toAjax(sysSubProjectFolderService.insertSysSubProjectFolder(sysSubProjectFolder));
    }

    /**
     * 修改子项目文件夹
     */
    @PreAuthorize("@ss.hasPermi('docdraw:subProjectFolder:edit')")
    @Log(title = "子项目文件夹", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysSubProjectFolder sysSubProjectFolder)
    {

        Long subProjectId = sysSubProjectFolder.getSubProjectId();
        SysSubProject sysSubProject = sysSubProjectService.selectSysSubProjectBySubProjectId(subProjectId);
        String status = sysSubProject.getStatus();
        if(UserConstants.USER_DISABLE.equals(status)){
            return error("项目 ["+sysSubProject.getSubProjectName()+"] 已被暂停 请联系管理员");
        }

        // 检查在同一子项目下是否存在重名
        if (!sysSubProjectFolderService.checkSubProjectFolderNameUnique(sysSubProjectFolder)) {
            return error("项目 ["+sysSubProject.getSubProjectName()+"] 已存在同名文件夹 请见检查");
        }

        sysSubProjectFolder.setUpdateBy(getUsername());
        return toAjax(sysSubProjectFolderService.updateSysSubProjectFolder(sysSubProjectFolder));
    }

    /**
     * 删除子项目文件夹
     */
    @PreAuthorize("@ss.hasPermi('docdraw:subProjectFolder:remove')")
    @Log(title = "子项目文件夹", businessType = BusinessType.DELETE)
	@DeleteMapping("/{subFolderIds}")
    public AjaxResult remove(@PathVariable Long[] subFolderIds)
    {
        return toAjax(sysSubProjectFolderService.deleteSysSubProjectFolderBySubFolderIds(subFolderIds));
    }
}
